package com.chinatgl.latte.ec.sign;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chinatgl.latte.delegates.LatteDelegate;
import com.chinatgl.latte.ec.API;
import com.chinatgl.latte.ec.R;
import com.chinatgl.latte.ec.R2;
import com.chinatgl.latte.ec.params.MapUtil;
import com.chinatgl.latte.net.NetRequest;
import com.chinatgl.latte.net.RestCreator;
import com.chinatgl.latte.net.listener.onNetRquestListener;
import com.chinatgl.latte.util.string.StringUtils;

import java.util.WeakHashMap;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 文件描述:登录界面
 * 作者: Created by 杨伟乔 on 2017/8/8 0008.
 * 版本号: 1.0
 */

public class SignInDelegate extends LatteDelegate {
    @BindView(R2.id.username)
    AutoCompleteTextView username;
    @BindView(R2.id.password)
    EditText password;
    @BindView(R2.id.sign_in_button)
    TextView signInButton;
    @BindView(R2.id.register)
    TextView register;
    @BindView(R2.id.forgot_password)
    TextView forgotPassword;
    @BindView(R2.id.login_weixin)
    ImageView loginWeixin;
    @BindView(R2.id.phone_login_form)
    LinearLayout phoneLoginForm;

    @Override
    public Object setLayout() {
        return R.layout.delegate_sign_in;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {

    }

    private ISignListener iSignListener = null;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof ISignListener) {
            iSignListener = (ISignListener) activity;

        }

    }

    /**
     * 点击事件
     */
    @OnClick({R2.id.sign_in_button, R2.id.register, R2.id.forgot_password, R2.id.login_weixin})
    public void onClick(View view) {

        switch (view.getId()) {
            case R2.id.sign_in_button:
                //登录
                //检查信息是否完整
                if (!StringUtils.isMobileNO(username.getText().toString())) {
                    //手机格式有误
                    signInButton.setText("手机号码有误");
                    return;
                }
                if (!StringUtils.rexCheckPassword(password.getText().toString())) {
                    //密码不符合要求
                    signInButton.setText("密码错误");
                    return;
                }
                //提交登录
                signInButton.setEnabled(false);
                login(username.getText().toString().trim(), password.getText().toString().trim());
                break;
            case R2.id.register:
                //注册 跳转到注册的界面
                start(new SignUpDelegate());
                break;
            case R2.id.forgot_password:
                //忘记密码 跳转到重置密码的界面

                break;
            case R2.id.login_weixin:

                loginWeixin.setEnabled(false);
                //微信登录

                break;
        }
    }

    /****
     * 登录方法
     */
    private void login(String username, String password) {
        WeakHashMap<String, Object> map = MapUtil.getParams();
        map.put("username", username);
        map.put("password", StringUtils.toMD5(password));
        NetRequest.post(API.LOGIN, map, null, false, 0, new onNetRquestListener() {
            @Override
            public void requestdataFinish(String catalog, Object bean) {
                SignHandler.onSignIn((String) bean, iSignListener);
            }

            @Override
            public void requestdataFailed(String msg, Throwable e) {
                System.out.println("网络请求错误:" + e);
            }

            @Override
            public void requestComplete() {
                System.out.println("网络请求完成:");
                signInButton.setEnabled(true);
            }
        },getActivity());

    }
}
